package mq

import (
	"gitee.com/mkwchecking/sr-core/yal/ymap"
	"gitee.com/mkwchecking/sr-core/yerr"
)

type MQFactory func(Conn) (IMsgQueue, error)

var _register = ymap.New[MQEngine, MQFactory]()

func Register(engine MQEngine, factory MQFactory) {
	_register.Register(engine, factory)
}

func Unregister(engine MQEngine) {
	_register.Unregister(engine)
}

func NewConn(conn Conn) (IMsgQueue, error) {
	factory, ok := _register.Get(conn.Engine)
	if !ok {
		return nil, yerr.ErrDataNotfound.Error()
	}

	return factory(conn)
}
